package cn.neusoft.dao.impl;

import cn.neusoft.bean.Cart;
import cn.neusoft.dao.ICartDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.CartQuery;
import cn.neusoft.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDao implements ICartDao {
    private QueryRunner queryRunner;

    public CartDao(){
        queryRunner = JdbcUtils.getQueryRunner();
    }
    @Override
    public int add(Cart cart) throws SQLException {
        String sql = "insert into t_cart ( foodId, businessId, userId, quantity) values(?,?,?,?)";
        return queryRunner.update(sql,
                cart.getFoodId(),
                cart.getBusinessId(),
                cart.getUserId(),
                cart.getQuantity());
    }

    @Override
    public int update(Cart cart) throws SQLException {
        String sql = "update t_cart set  foodId=?, businessId=?, userId=?, quantity=? where cartId=?";
        return queryRunner.update(sql,
                cart.getFoodId(),
                cart.getBusinessId(),
                cart.getUserId(),
                cart.getQuantity(),
                cart.getCartId());
    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from t_cart where cartId=?";
        return queryRunner.update(sql, id);

    }

    @Override
    public Cart getById(Integer id) throws SQLException {
        String sql = "select cartId,foodId, businessId, userId, quantity from t_cart where cartId=?";
        return queryRunner.query(sql, new BeanHandler<Cart>(Cart.class), id);
    }

    @Override
    public List<Cart> getAll() throws SQLException {
        String sql = "select cartId,foodId, businessId, userId, quantity from t_cart";
        return queryRunner.query(sql, new BeanListHandler<Cart>(Cart.class));
    }

    @Override
    public int getCount(BaseQuery baseQuery) throws SQLException {
        CartQuery cartQuery = (CartQuery) baseQuery;

        List<Object> params = new ArrayList<>();

        String sql = "select count(cartId) from t_cart where 1=1";
        if (cartQuery.getFoodId()!=null){
            sql += " and foodId like concat('%',?,'%')";
            params.add(cartQuery.getFoodId());
        }
        System.out.println("查询总记录："+sql);
        System.out.println("参数集合："+params);

        return queryRunner.query(sql, new ScalarHandler<Long>(),params.toArray()).intValue();
    }

    @Override
    public List<Cart> getByPage(BaseQuery baseQuery) throws SQLException {

        CartQuery cartQuery = (CartQuery) baseQuery;

        List<Object> params = new ArrayList<>();

        String sql = "select cartId,foodId, businessId, userId, quantity from t_cart where 1=1 " ;
        if (cartQuery.getFoodId()!=null){
            sql += " and foodId like concat('%',?,'%')";
            params.add(cartQuery.getFoodId());
        }
        sql+="order by cartId desc limit ?,?";

        params.add(cartQuery.getStartIndex());
        params.add(cartQuery.getPageCount());

        System.out.println("查询集合："+sql);
        System.out.println("参数集合："+params);

        return queryRunner.query(sql,new BeanListHandler<Cart>(Cart.class),params.toArray());

    }
}
